
import _pg
import project, tclosure

def createTables(db):
    query = """
CREATE TABLE Threads(
	MessageID varchar(200) references Messages(MessageID) NOT NULL,
	ResponseID varchar(200) references Messages(MessageID) NOT NULL,
	distance int NOT NULL,
PRIMARY KEY (MessageID, ResponseID)
)
"""
    dropq = "drop table Threads cascade"
    print "dropping tables"
    try:
	    db.query(dropq)
    except _pg.ProgrammingError:
	    print "could not drop tables, maybe they're not in there yet..."
    try:
        db.query(query)
    except _pg.ProgrammingError, e:
        raise project.ProjectError("db error:" + e)

def start(proj, log):
    db = proj.getDatabase()
    createTables(db)
    tclosure.calcThreads(db, log)   
